package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.JurisdictionDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class JurisdictionServiceImpl implements JurisdictionService {
    @Autowired
    private JurisdictionDao jurisdictionDao;

    @Override
    public PageResult findByPage(QueryPageBean queryPageBean) {

        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        Page<Permission> findByPage = jurisdictionDao.findByPage(queryPageBean.getQueryString());
        if (findByPage.size() <= 0) {
            PageHelper.startPage(queryPageBean.getCurrentPage()-1, queryPageBean.getPageSize());
            Page<Permission> findByPageUpdate = jurisdictionDao.findByPage(queryPageBean.getQueryString());
            return new PageResult(findByPage.getTotal(), findByPageUpdate);
        }
        PageResult pageResult = new PageResult(findByPage.getTotal(), findByPage);
        return pageResult;
    }

    @Override
    public boolean add(String name, String keyword, String description, Integer[] checkitemIds) {
//        首先判断名字是否有相同可能会有多个名字编码不同  不知道使用那个
        Long findName = jurisdictionDao.nameQuery(name);
        if (findName > 0) {
            return false;
        }
        // 在判断编码是否存在相同
        Long findKeyWord = jurisdictionDao.keywordQuery(keyword);
        if (findKeyWord > 0) {
            return false;
        }
        Permission permission = new Permission();
        permission.setName(name);
        permission.setKeyword(keyword);
        permission.setDescription(description);
        jurisdictionDao.add(permission);
//        添加关系依赖
        if ( checkitemIds != null) {
            for (Integer integer : checkitemIds) {
                jurisdictionDao.addRole(permission.getId(), integer);
            }
        }
        return true;
    }

    @Override
    public void delById(Integer id) {
//        删除中间表关系
        jurisdictionDao.deleteRelationship(id);
        jurisdictionDao.delById(id);
    }

    @Override
    public Permission findById(Integer id) {
        Permission permission = jurisdictionDao.findById(id);
        return permission;
    }

    @Override
    public List<Role> findAll() {
        List<Role> roles = jurisdictionDao.findAll();
        return roles;
    }

    @Override
    public Map<String, Object> findByIdRole(Integer id) {
//        查询所有
        List<Role> roles = jurisdictionDao.findAllRole();
//        根据id进行查询返回数组
        Integer[] integers = jurisdictionDao.findByIdRole(id);
        HashMap<String, Object> map = new HashMap<>();
        map.put("list", roles);
        map.put("arrays", integers);
        return map;
    }

    @Override
    public void edit(Integer[] checkitemIds, Permission permission) {
//        调用修改权限
        jurisdictionDao.updatePermission(permission);
//        删除管理 重新设置关系
        jurisdictionDao.delRelation(permission.getId());
        for (Integer checkitemId : checkitemIds) {
            jurisdictionDao.addRole(permission.getId(), checkitemId);
        }
    }

/**
 * @program: itcast_health
 *
 * @description:
 *
 * @author: 李先生
 *
 * @create: 2020-07-07 18:31
 **/

}
